package com.zxt.content.copywriting.controller;

import com.zxt.bean.Result;
import com.zxt.user.api.utils.UserContainer;
import com.zxt.bean.UserInfoVO;
import com.zxt.utils.ZXTUtils;
import com.zxt.content.copywriting.req.CopywritingTypeDTO;
import com.zxt.content.copywriting.resp.CopywritingTypeVO;
import com.zxt.content.copywriting.service.ICopywritingTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @author zxt
 */
@Slf4j
@RequestMapping("/copywriting/copywritingType/")
@RestController
@AllArgsConstructor
@Api(tags = "文案类型管理接口")
public class CopywritingTypeController {

    private final ICopywritingTypeService copywritingTypeService;

    @GetMapping("/query")
    @ApiOperation(value = "查询", notes = "查询")
    private Result<List<CopywritingTypeVO>> index() {
        return Result.newSuccess(copywritingTypeService.queryList());
    }

    @PostMapping("/addOrUpdate")
    @ApiOperation(value = "添加", notes = "添加")
    private Result addOrUpdate(@RequestBody CopywritingTypeDTO dto) {
        //从ThreadLocal里取出当前登录的用户信息
        UserInfoVO userInfoApiVO = UserContainer.USER_INFO_VO_THREAD_LOCAL.get();

        //使用函数式接口校验抛出异常
        ZXTUtils.isTrue(!userInfoApiVO.getIsAdmin()).throwMessage("没有权限修改文案类型");

        //使用函数式接口优化if else
        ZXTUtils.isTrueOrFalse(Objects.isNull(dto.getId())).trueOrFalseHandle(() -> {
            copywritingTypeService.add(dto);
        }, () -> {
            copywritingTypeService.update(dto);
        });
        return Result.newSuccess();
    }
}
